import java.util.Random;

public class Sort {
    public static void main(String[] args) {
        Random random = new Random();
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(0,500);
            System.out.print(arr[i]+" ");
        }
        int[] arr1 = arr;
        System.out.println();

        sort1(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        mergeSort(arr1);
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
    }
    public static void sort1(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(arr[j]>tmp){
                    arr[j+1] = arr[j];
                }else {
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

    public static void shellSort(int[] arr){
        int gap = arr.length;
        while (gap>1){
            gap = gap/2;
            shell(arr,gap);
        }
    }
    public static void shell(int[] arr,int gap){
        int tmp;
        for (int i = gap; i <arr.length ; i++) {
            tmp = arr[i];
            int j = i-gap;
            for (; j >=0 ; j+=gap) {
                if(arr[j]>tmp){
                    arr[j+gap] = arr[j];
                }else {
                    break;
                }
            }
            arr[j+gap] = tmp;
        }
    }


    public static void selectSort(int[] arr){
        //选择排序
        for (int i = 0; i < arr.length ; i++) {
            int flag = i;//记录下标
            for (int j = i+1; j < arr.length ; j++) {
                if (arr[j]<arr[flag]){
                    flag=j;
                }
            }
            int tmp = arr[i];
            arr[i] = arr[flag];
            arr[flag] = tmp;
        }
    }

    public static void selectSort2(int[] arr){
        int left = 0;
        int right = arr.length-1;
        while (left<right){
            int i = left+1;
            int minIndex = left;
            int maxIndex = left;
            while (i<=right){
                if(arr[i]<arr[minIndex]){
                    minIndex = i;
                }
                if(arr[i]>arr[maxIndex]){
                    maxIndex = i;
                }
                i++;
            }
            int tmp = arr[right];
            arr[right] = arr[maxIndex];
            arr[maxIndex] = tmp;
            tmp = arr[left];
            arr[left] = arr[minIndex];
            arr[minIndex] = tmp;

            left++;
            right--;
        }
    }


    public static void heapSort(int[] arr){
        //创建大根堆
        createHeap(arr);
        int end = arr.length-1;
        while (end>0){
            int tmp = arr[0];
            arr[0] = arr[end];
            arr[end] = tmp;
            siftDown(arr,0,end);
            end--;
        }
    }
    private static void createHeap(int[] arr){
         //从最后一个子树的父亲节点开始
        for (int parent = (arr.length-1-1)/2; parent >=0 ; parent--) {
            siftDown(arr,parent,arr.length);
        }
    }
    private static void siftDown(int[] arr,int parent,int len){
        //向下调整
        int child = (2*parent)+1;
        while (child<len){
            if(child+1<len&&arr[child]<arr[child+1]){
                child = child+1;
            }
            if (arr[child]>arr[parent]){
                int tmp = arr[child];
                arr[child] = arr[parent];
                arr[parent] = tmp;
                parent = child;
                child = 2*parent+1;
            }else{
                break;
            }
        }
    }


    public static void bubbleSort(int[] array){
        //i用来表示趟数
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length-1; j++) {
                if(array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if(flag==false){
                //有序
                break;
            }
        }
    }
    private static void swap(int[] arr,int i,int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }


    public static void fastSort(int[] arr){
        sort3(arr,0,arr.length-1);
    }
    private static void sort3(int[] arr,int start,int end){
        if(start>=end){
            return;
        }
        //int flag = partitionHoare(arr,start,end);

        //三数取中,中间大的数字的下标
        int index = middleNum(arr,start,end);
        swap(arr,index,start);

        int flag = partition(arr,start,end);
        sort3(arr,start,flag-1);
        sort3(arr,flag+1,end);

    }
    private static int middleNum(int[] arr,int left,int right){
        int mid = left+((right-left)>>1);

        if (arr[left]<arr[right]){
            if (arr[mid]<arr[left])
                return left;
            else if (arr[mid]>arr[right])
                return right;
            else
                return mid;
        }else{
            if (arr[mid]<arr[right])
                return right;
            else if (arr[mid]>arr[left])
                return left;
            else
                return mid;
        }
    }
    private static int partitionHoare(int[] arr,int left,int right){
        int temp = arr[left];
        int flag = left;
        while (left<right){
            while (left<right&&arr[right]>=temp)
                right--;
            while (left<right&&arr[left]<=temp)
                left++;
            swap(arr,left,right);
        }
        swap(arr,left,flag);
        return left;
    }

    private static int partition(int[] arr,int left,int right){
        int temp = arr[left];
        int flag = left;
        while (left<right){
            while (left<right&&arr[right]>=temp)
                right--;
            while (left<right&&arr[left]<=temp)
                left++;
            if (left>=right){
                break;
            }
            arr[flag] = arr[right];
            arr[right] = arr[left];
        }
        arr[left] = temp;
        return left;
    }




    //归并
    public static void mergeSort(int[] arr){
        mergeFunc(arr,0,arr.length-1);
    }
    private static void mergeFunc(int[] arr,int left,int right){
        if (left>=right){
            return;
        }
        int mid = left+((right-left)>>1);
        //分组
        mergeFunc(arr,left,mid);
        mergeFunc(arr,mid+1,right);
        merge(arr,left,mid,right);
    }
    private static void merge(int[] arr,int left,int mid,int right){
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int[] tmp = new int[right-left+1];
        int flag = 0;
        //1.保证两个表都有数据
        while (s1<=e1&&s2<=e2){
            if(arr[s1]<=arr[s2]){
                tmp[flag] = arr[s1];
                s1++;
            }else {
                tmp[flag] = arr[s2];
                s2++;
            }
            flag++;
        }
        //剩余数据
        while (s1<=e1){
            tmp[flag] = arr[s1];
            flag++;
            s1++;
        }
        while (s2<=e2){
            tmp[flag] = arr[s2];
            flag++;
            s2++;
        }

        //返回到原数组
        for (int i = 0; i <flag ; i++) {
            arr[i+left] = tmp[i];
        }
    }
}
